package com.esms.controller;

import com.esms.dao.EmployeeMapper;
import com.esms.po.Bonus;
import com.esms.po.Employee;
import com.esms.po.Fassessment;
import com.esms.po.Grand;
import com.esms.service.ILogService;
import com.esms.service.impl.EmployeeBonusServerImpl;
import com.esms.vo.EmployeeBonusVO;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author 帅帅帅帅
 *
 */

@Controller
public class EmployeeBonusController {

	@Autowired
	ILogService logService;
	
	@Autowired
	private EmployeeBonusServerImpl EmployeeBonusService;
	
	@Autowired
    private EmployeeMapper employeeMapper = null;
	
    @RequestMapping("employeeBonus.do")
    @ResponseBody
    public EmployeeBonusVO findSelectiveGrand(
    		@RequestParam(value="d_id", defaultValue="0") int fdid,
            @RequestParam(value="e_id", defaultValue="0") String fEid,
			HttpSession httpSession) throws Exception {

		Integer eId = (Integer) httpSession.getAttribute("employeeId");
		// pageNum:起始页面 pageSize:每页的大小
		// PageHelper.startPage(pageNum,limit);
		// 查找条件，一定要紧跟在startPage后
	    List<Bonus> bonusList = new ArrayList<Bonus>();
	    try{
		    Employee employee = employeeMapper.selectByPrimaryKey(eId);
		    String e_account = employee.geteAccount();
		    String accountDB = EmployeeBonusService.getDBaccount();
		    if (e_account.equals(accountDB)) {
			    bonusList = EmployeeBonusService.getEmployeeBonusList(e_account, fdid, fEid);
		    }
	    }catch (Exception e){
	    	e.printStackTrace();
	    }

		// 设置前台需要的数据
		EmployeeBonusVO employeeBonusVO = new EmployeeBonusVO();
		employeeBonusVO.setCode(0);
		employeeBonusVO.setMsg("");
		employeeBonusVO.setCount(bonusList.size());
		employeeBonusVO.setData(bonusList);
		return employeeBonusVO;
	}
    
    @RequestMapping("updateBonus.do")
    @ResponseBody
    public EmployeeBonusVO updateBonus(
    		@RequestParam("list") String list,
            HttpSession httpSession) throws Exception {
    	int req = 0;
    	String reMsg;
    	ObjectMapper mapper = new ObjectMapper();
    	JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, Bonus.class);
    	List<Bonus> listBonus =  (List<Bonus>)mapper.readValue(list, jt);
    	/*我的工号查询*/
    	Integer eId = (Integer) httpSession.getAttribute("employeeId");
    	Employee employee = employeeMapper.selectByPrimaryKey(eId);
    	reMsg = "评价成功";
		logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
				"加分项考核评价成功");
    	//获取系统当前时间
    	String quarter = null;
        String systemTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH)+1;
        if(month>=1 && month<=3) {
        	quarter = year + "01";
        }else if (month>=4 && month<=6) {
        	quarter = year + "02";
        }else if (month>=7 && month<=9) {
        	quarter = year + "03";
        }else if (month>=10 && month<=12) {
        	quarter = year + "04";
        }
        Fassessment fassessment = new Fassessment();
    	for(int i=0;i<listBonus.size();i++) {
    		Bonus grandBonus = listBonus.get(i);
    		/*输入分数判断*/
    		
    		/*设置同级评分表*/
    		fassessment.setfAuserid(grandBonus.getDid());
    		fassessment.setfAccount(employee.geteAccount());
    		fassessment.setfDuserid(grandBonus.geteAccount());
    		fassessment.setfCreateTime(systemTime);
    		fassessment.setfCreateQuarter(quarter);
    		fassessment.setfScore(grandBonus.getfScore());
    		fassessment.setfRmk(grandBonus.getfRmk());
    		
    		req = EmployeeBonusService.deleteBonus(employee.geteAccount(),grandBonus.geteAccount());
    		if(req < 0) {
       		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"加分项考核评价失败");
    		}
    		req = EmployeeBonusService.insertBonus(fassessment);
    		if(req < 0) {
          		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"加分项考核评价失败");
       		}
        }

        //设置前台需要的数据
        EmployeeBonusVO employeeBonusVO = new EmployeeBonusVO();
        employeeBonusVO.setCode(0);
        employeeBonusVO.setMsg(reMsg);
        employeeBonusVO.setData(listBonus);
        return employeeBonusVO;
    }
    public int checkNumber(Grand grand) {
    	return 0;
    }
}
